<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
function myNew(constructor, ...args) {
  // 创建一个对象
  let obj = {};
  // 构造函数的prototype属性
  let prototype = constructor.prototype;

  // 设置对象原型
  Object.setPrototypeOf(obj, prototype);

  //  执行构造函数，利用apply修改this执行
  let result = constructor.apply(prototype, args);
  
  // 如果构造函数返回一个对象，则返回该对象，否则返回新创建的对象
  return typeof result === 'object' && result !== null ? result : obj

}

function Person(name){
  this.name = name
}

// console.log('re',myNew(Person,'lili'));
let p = myNew(Person,'lili')
console.log(p.__proto__ === Person.prototype);// true


</script>

</html>